jQuery(function ($) {
    jQuery.ajaxSetup({cache: false});
});
/*重置ajax并取消缓存*/

//标准结果,读取器
var myloader = function (param, success, error) {
    var me = $(this);
    var opts = me.datagrid("options");
    if (!opts.url) {
        return false;
    }
    $.ajax({
        type: opts.method,
        url: opts.url,
        data: param,
        dataType: "json",
        success: function (data) {
            if (!data.success) {
                error.apply(this, arguments);
            } else {
                success({
                    total: data.pagination.count,
                    rows: data.data
                });
            }
        },
        error: function () {
            error.apply(this, arguments);
        }
    });
};

/*组件默认值参数*/
if ($.fn.combobox) {
    $.fn.combobox.defaults.width = 200;
    $.fn.combobox.defaults.labelWidth = 85;
    $.fn.combobox.defaults.panelHeight = 'auto';
    $.fn.combobox.defaults.editable = false;
    $.fn.combobox.defaults.labelAlign = 'right';
}

if ($.fn.textbox) {
    $.fn.textbox.defaults.width = 200;
    $.fn.textbox.defaults.labelWidth = 85;
    $.fn.textbox.defaults.labelAlign = 'right';
}

if ($.fn.datebox) {
    $.fn.datebox.defaults.width = 200;
    $.fn.datebox.defaults.labelWidth = 85;
    $.fn.datebox.defaults.labelAlign = 'right';
}

if ($.fn.datagrid) {
    $.fn.datagrid.defaults.loader = myloader;
    $.fn.datagrid.defaults.method = 'get';
    $.fn.datagrid.defaults.pagination = true;
    $.fn.datagrid.defaults.rownumbers = true;
    $.fn.datagrid.defaults.pageList = [10, 30, 50, 100];
    $.fn.datagrid.defaults.remoteSort = true;
    $.fn.datagrid.defaults.striped = true;
    //$.fn.datagrid.defaults.fitColumns = true;
    $.fn.datagrid.defaults.fit = true;
    $.fn.datagrid.defaults.singleSelect = true;
}

/*初始化语言*/
if ($.fn.pagination) {
    $.fn.pagination.defaults.beforePageText = '第';
    $.fn.pagination.defaults.afterPageText = '共 {pages} 页';
    $.fn.pagination.defaults.displayMsg = '显示 {from} 到 {to},共 {total} 记录';
}
if ($.fn.datagrid) {
    $.fn.datagrid.defaults.loadMsg = '正在处理，请稍待。。。';
}
if ($.fn.treegrid && $.fn.datagrid) {
    $.fn.treegrid.defaults.loadMsg = $.fn.datagrid.defaults.loadMsg;
}
if ($.messager) {
    $.messager.defaults.ok = '确定';
    $.messager.defaults.cancel = '取消';
}
$.map(['validatebox', 'textbox', 'passwordbox', 'filebox', 'searchbox',
    'combo', 'combobox', 'combogrid', 'combotree',
    'datebox', 'datetimebox', 'numberbox',
    'spinner', 'numberspinner', 'timespinner', 'datetimespinner'], function (plugin) {
    if ($.fn[plugin]) {
        $.fn[plugin].defaults.missingMessage = '必填';
    }
});
if ($.fn.validatebox) {
    $.fn.validatebox.defaults.rules.email.message = '请输入有效的电子邮件地址';
    $.fn.validatebox.defaults.rules.url.message = '请输入有效的URL地址';
    $.fn.validatebox.defaults.rules.length.message = '输入内容长度必须介于{0}和{1}之间';
    $.fn.validatebox.defaults.rules.remote.message = '请修正该字段';
}
// 增加日期、时间格式验证规则
$.extend($.fn.validatebox.defaults.rules, {
    // 日期验证
    date: {
        validator: function (value, param) {
            //格式yyyy-MM-dd或yyyy-M-d
            return /^(?:(?!0000)[0-9]{4}([-]?)(?:(?:0?[1-9]|1[0-2])\1(?:0?[1-9]|1[0-9]|2[0-8])|(?:0?[13-9]|1[0-2])\1(?:29|30)|(?:0?[13578]|1[02])\1(?:31))|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)([-]?)0?2\2(?:29))$/i.test(value);
        },
        message: '请输入合适的日期格式'
    },
    // 日期时间验证
    datetime: {
        validator: function (value, param) {
            // 标准时间格式: yyyy-MM-dd HH:mm:ss
            var regStandard = /^\1|2\d{3}-\d{1,2}-\d{1,2} \d{1,2}:\d{1,2}:\d{1,2}$/;

            return regStandard.test(value);
        },
        message: '请输入合适的日期时间格式'
    }
});
if ($.fn.calendar) {
    $.fn.calendar.defaults.weeks = ['日', '一', '二', '三', '四', '五', '六'];
    $.fn.calendar.defaults.months = ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'];
}
if ($.fn.datebox) {
    $.fn.datebox.defaults.currentText = '今天';
    $.fn.datebox.defaults.closeText = '关闭';
    $.fn.datebox.defaults.okText = '确定';
    $.fn.datebox.defaults.formatter = function (date) {
        var y = date.getFullYear();
        var m = date.getMonth() + 1;
        var d = date.getDate();
        return y + '-' + (m < 10 ? ('0' + m) : m) + '-' + (d < 10 ? ('0' + d) : d);
    };
    $.fn.datebox.defaults.parser = function (s) {
        if (!s) return new Date();
        var ss = s.split('-');
        var y = parseInt(ss[0], 10);
        var m = parseInt(ss[1], 10);
        var d = parseInt(ss[2], 10);
        if (!isNaN(y) && !isNaN(m) && !isNaN(d)) {
            return new Date(y, m - 1, d);
        } else {
            return new Date();
        }
    };
}
if ($.fn.datetimebox && $.fn.datebox) {
    $.extend($.fn.datetimebox.defaults, {
        currentText: $.fn.datebox.defaults.currentText,
        closeText: $.fn.datebox.defaults.closeText,
        okText: $.fn.datebox.defaults.okText
    });
}
if ($.fn.datetimespinner) {
    $.fn.datetimespinner.defaults.selections = [[0, 4], [5, 7], [8, 10], [11, 13], [14, 16], [17, 19]]
}


/*初始化*/
//待修复 组件设置label后，高度失效问题
$.extend($.fn.tabs.defaults, {tabHeight: 32});
$.extend($.fn.textbox.defaults, {height: 32});
$.extend($.fn.passwordbox.defaults, {height: 32});
$.extend($.fn.combo.defaults, {height: 32});
$.extend($.fn.combobox.defaults, {height: 32});
$.extend($.fn.combogrid.defaults, {height: 32});
$.extend($.fn.combotree.defaults, {height: 32});
$.extend($.fn.combotreegrid.defaults, {height: 32});
$.extend($.fn.menu.defaults, {itemHeight: 32});

$.extend($.messager.defaults, {width: 320});


$.extend($.fn.datebox.defaults, {
    height: 32,
    panelWidth: 220
    /*
     此处待修复，无法选择日期错误
     ,
     formatter:function(date){
     var y = date.getFullYear();
     var m = date.getMonth()+1;
     var d = date.getDate();
     return y+"年"+m+"月"+d+"日";
     }*/
});
$.extend($.fn.datetimebox.defaults, {
    height: 32,
    panelWidth: 220
    /*
     此处待修复，无法选择日期错误
     ,
     formatter:function(date){
     var y = date.getFullYear();
     var m = date.getMonth()+1;
     var d = date.getDate();
     var H = date.getHours();       //获取当前小时数(0-23)
     var i = date.getMinutes();     //获取当前分钟数(0-59)
     var s = date.getSeconds();     //获取当前秒数(0-59)
     return y+"年"+m+"月"+d+"日 "+H+":"+i+":"+s;
     }*/
});
$.extend($.fn.datetimespinner.defaults, {height: 32});
$.extend($.fn.numberbox.defaults, {height: 32});
$.extend($.fn.spinner.defaults, {height: 32});


$.extend($.fn.numberspinner.defaults, {height: 32});//上线数字不能使用
$.extend($.fn.searchbox.defaults, {height: 32});
$.extend($.fn.filebox.defaults, {height: 32});

$.extend($.fn.validatebox.defaults, {height: 32, tipPosition: 'bottom'});
$.extend($.fn.validatebox.defaults.tipOptions, {
    tipPosition: 'bottom',
    onShow: function () {
        $(this).tooltip("tip").css({
            color: "#fff",
            border: "none",
            backgroundColor: "#ff7e00"
        });
    }
}); //重置tipOptions的样式

$.extend($.fn.tooltip.defaults, {
    position: 'right',
    onShow: function () {
        $(this).tooltip("tip").css({
            color: "#fff",
            border: "none",
            backgroundColor: "#ff7e00"
        });
    }
});

function getFormJson(form) {
    var o = {};
    var a = $(form).serializeArray();
    $.each(a, function () {
        if (o[this.name] !== undefined) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });
    return o;
}


// 切换高级筛选
function toggleSearchPanel(that) {
    var icon = $(that).find(".l-btn-text .yvan-icon");
    var oText = $(that).find(".l-btn-text");
    $(that).toggleClass("yvan-z-open");
    if ($(that).hasClass("yvan-z-open")) {
        oText.html("收起 ").append(icon);
    } else {
        oText.html("查询 ").append(icon);
    }
    $('#YvanSearchBar').slideToggle(function () {
        $(window).trigger("resize");
    });
}

